<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity {
	private $_id;
	public function authenticate($crypt = true) {
		$user = User::model()->findByAttributes(array('username' => $this->username));
	    if (!is_object($user)) {
	        $this->errorCode = self::ERROR_USERNAME_INVALID;
	    } else if ( ($crypt && $user->password !== crypt($this->password, $user->password)) || (!$crypt && $user->password!==$this->password)) {
	        $this->errorCode = self::ERROR_PASSWORD_INVALID;
	    } else if(!$user->status){
	    	$this->errorCode = 3;
	    } else {
	        $this->_id = $user->id;
	        $this->setState('title', $user->username);
	        $this->setState('current_user', $user);
	        $this->errorCode = self::ERROR_NONE;
	    }
	    return $this->errorCode;
	}

	public function getId(){
		return $this->_id;
	}

}